package cn.kgc.controller;

import cn.kgc.pojo.Salary;
import cn.kgc.pojo.SalaryReason;
import cn.kgc.service.SalartService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 海宁
 * @date: 2022/1/24
 * @description:
 */
@Controller
@RequestMapping("/sal")
public class HRController {


    @Resource
    private SalartService salartService;

    Date date = new Date();
    /**
     * 跳转查询页面
     * @param
     * @return
     */

    @RequestMapping("/sel")
    public String selsal1(String userRole){
        return "salarysel";
    };

    /**
     * 跟据条件查询
     * @param model
     * @param payStandardCode
     * @param checkState
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/selby")
    @ResponseBody
    public String selby(Model model, @RequestParam(name="payStandardCode",required = false) String payStandardCode,
                        @RequestParam(name="checkState",required = false) Integer checkState,
                        @RequestParam(name="startDate",required = false) String startDate,
                        @RequestParam(name = "endDate",required = false) String endDate
                       ){

        List<Salary> list = salartService.selsal(payStandardCode,checkState,startDate,endDate);
//        Integer listnum = list.size();
//        System.out.println(list.size()+"1111111111111111111111111111111111111111111111111111111111111111111111111");
//        session.removeAttribute("aaa");
//        session.setAttribute("aaa",listnum);
//        if(list.size()==0){
//            session.setAttribute("error","没有查到结果，请重新选择查询条件");
//        }
        return JSON.toJSONString(list);
    };

    /**
     * 根据id查询明细
     * @param id
     * @param model
     * @param sum
     * @return
     */
    @RequestMapping(value = "/selbyid/{id}")
    public String selbyid(@PathVariable(name="id") Integer id,Model model,Double sum){

        Salary salary = salartService.selsalbyid(id);
        sum = salary.getRegularWage()+salary.getPerformanceBonus()
                +salary.getTransportation()+salary.getPhoneAllowance()
                +salary.getMealSubsidy()+salary.getHousingAllowance()
                +salary.getTravelAllowance()+salary.getOvertimeAllowance();
        model.addAttribute("sum",sum);
        model.addAttribute("salary",salary);
        return "selsal";
    }

    @RequestMapping("/agree")
    public String selagree(){
        return "salagreebohui";
    }
    /**
     * 根据id查询明细
     * @param id
     * @param model
     * @param sum
     * @return
     */
    @RequestMapping(value = "/selagreeid/{id}")
    public String selagreeid(@PathVariable(name="id") Integer id,Model model,Double sum){
        SalaryReason salary1=salartService.selSr(id);
        Salary salary = salartService.selsalbyid(id);
        sum = salary.getRegularWage()+salary.getPerformanceBonus()
                +salary.getTransportation()+salary.getPhoneAllowance()
                +salary.getMealSubsidy()+salary.getHousingAllowance()
                +salary.getTravelAllowance()+salary.getOvertimeAllowance();
        model.addAttribute("sum",sum);
        model.addAttribute("salary",salary);
        model.addAttribute("salary1",salary1);
        return "salagreebohui";
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public  Boolean del(Integer id){
        Salary salary = salartService.selsalbyid(id);
        salary.setCheckState("删除");
        Boolean bo = salartService.update(salary);
        if(bo){
            return true;
        }
        return false;

    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model){
        int code = salartService.sellast();
        int newcode = code+1;
        model.addAttribute("code",newcode);
        return "addsal";
    }

    /**
     * 对添加数据进行保存
     * @param salary
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Boolean save(@ModelAttribute Salary salary,Model model){
        salary.setParroll(salary.getPerformanceBonus()+salary.getTransportation()+salary.getPhoneAllowance()+salary.getMealSubsidy()+salary.getTravelAllowance()+salary.getHousingAllowance()+salary.getRegularWage());
        salary.setId(-1);
        Boolean bo2 = salartService.addsal(salary);
        if(bo2){
            Boolean bo = true;
            List<Salary> list = salartService.selsal(null,0,null,null);
            Salary salary1 = list.get(0);
            int sid=salary1.getId();

            SalaryReason sr2 = new SalaryReason(-1, sid, null);
            salartService.addreason(sr2);
        }




        model.addAttribute("Parroll",salary.getParroll());
        if(bo2){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 跳转修改页面
     * @return
     */
    @RequestMapping(value = "/upd/{id}")
    public String upde(@PathVariable(name="id") Integer id,Model model){

        Salary salary = salartService.selsalbyid(id);
        String str = salary.getCheckState();

        model.addAttribute("state", str);

        model.addAttribute("salary",salary);
        return "updsal";
    }

    @RequestMapping("/updsave")
    @ResponseBody
    public Boolean updsave(@ModelAttribute Salary salary,@RequestParam(required = false) String txt){
       Boolean bo = null;
        System.out.println(txt);

        int sid=salary.getId();
        SalaryReason sr= salartService.selSr(sid);
        if (sr==null){
            SalaryReason sr2 = new SalaryReason(-1,sid,txt);
            bo=salartService.addreason(sr2);
        }else{
            SalaryReason sr2 = new SalaryReason(sr.getId(),sid,txt);
            bo=salartService.updreason(sr2);
        }

        salary.setParroll(salary.getPerformanceBonus()+salary.getTransportation()+salary.getPhoneAllowance()+salary.getMealSubsidy()+salary.getTravelAllowance()+salary.getHousingAllowance()+salary.getRegularWage());
        if(salartService.update(salary)&&bo){
            return true;
        }
        return false;
    }
    @RequestMapping("/updrank")
    @ResponseBody
    public Boolean updrank(Integer id, String rank){
        Salary salary = new Salary();
        salary.setId(Integer.valueOf(id));
        salary.setCheckState(rank);
        if(salartService.update(salary)){
            return true;
        }
        return false;
    }

    /**
     * 查询名字唯一性.0
     * @return
     */
    @RequestMapping("/selname")
    @ResponseBody
    public Boolean selname(@RequestParam("nameOfSalary") String nameOfSalary) {
        System.out.println(nameOfSalary);
        List<Salary> list = salartService.selsal(null, 0, null, null);
        List list2 = new ArrayList();
        String[] str = new String[list.size()];
        for (Salary a : list) {
            list2.add(a.getNameOfSalary());
        }
        if(list2.contains(nameOfSalary)){
            return true;
        }
        return false;
    }

    @RequestMapping("/seldel")
    public String seldel(String userRole){
        return "salaryseldel";
    };
    /**
     * 跟据条件查询
     * @param model
     * @param payStandardCode
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/seldelby")
    @ResponseBody
    public String seldelby(Model model, @RequestParam(name="payStandardCode",required = false) String payStandardCode,
                        @RequestParam(name="startDate",required = false) String startDate,
                        @RequestParam(name = "endDate",required = false) String endDate){
        List<Salary> list = salartService.selsal(payStandardCode,5,startDate,endDate);
        for (Salary salary : list) {
            System.out.println(list+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        }
        return JSON.toJSONString(list);
    };

    /**
     * 删除更改状态
     * @param id
     * @return
     */
    @RequestMapping("/resave")
    @ResponseBody
    public  Boolean resave(Integer id){
        Salary salary = salartService.selsalbyid(id);
        salary.setCheckState("起草");
        Boolean bo = salartService.update(salary);
        if(bo){
            return true;
        }
        return false;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/finaldel")
    @ResponseBody
    public  Boolean finaldel(Integer id){
        Boolean bo = salartService.delsal(id);
        if(bo){
            return true;
        }
        return false;
    }
    /**
     * 清空
     * @return
     */
    @RequestMapping("/delall")
    @ResponseBody
    public  Boolean delall(){
        Boolean bo = salartService.delall();
        if(bo){
            return true;
        }
        return false;
    }


}
